<?php
/**
 * Project: HTPHouDuan
 * User: xilibo
 * Date: 2018/1/25
 */

namespace app\api\controller;

use think\Controller;
use think\Cache;
use think\Config;
use think\Exception;
use think\Cookie;
use think\Session;
use app\common\model\UserBetModel;

/**
 * 用户相关接口
 */ 
class UserController extends BaseController
{

    /**
     * 用户列表
     */
    public function index()
    {
        $page = $this->request->get('page', 1, 'intval');
        $pageSize = $this->request->get('psize', 1, 'intval');
        $orderBy = $this->request->get('order', '');
        $where = [
            'order' => $orderBy,
            'page' => $page,
            'page_size' => $pageSize,
        ];
        $users = model('user')->getList($where);

        return $users;
    }

    /**
     * 获取指定用户信息
     * @param  [type] $id 用户ID
     * @return [type]     [description]
     */
    public function read($id)
    {
        $user = model('user')->getRow(['id' => $id]);

        return $user;
    }

    /**
     * 额度转换
     */
    public function recharge()
    {
        $orderBn = $this->request->param('ordNo', '');
        $payType = trim($this->request->param('method', ''));
        $money = $this->request->param('money', 0, 'floatval');

        if (! in_array($payType, ['in', 'out'])){
            $payType = '';
        }

        if (empty($orderBn) || empty($payType) || empty($money)){
            return $this->jsonError('参数不能为空');
        }

        $userModel = model('user');
        $result = $userModel->recharge($this->curUser['id'], $orderBn, $payType, $money);
        if ($result == -1){
            return $this->jsonError('余额不足，请充值');
        } else if ($result == -2){
			return $this->jsonError('此订单已存在');
		} else if (! $result){
            return $this->jsonError('额度转换失败，请重试');
        }

        $message = ['data' => $result];
        return $this->jsonSuccess($message);
    }

    /**
     * 额度转换状态查询
     */ 
    public function myRecharge()
    {
        $orderBn = $this->request->get('ordNo', '');
        if (empty($orderBn)){
            $this->jsonError('订单号不能为空');
        }

        if(empty($this->curUser)){
            return $this->jsonError('用户不存在或没登陆');
        }

        $result = model('user_balance_log')->chkeckRecharge($orderBn, $this->curUser['id']);
        $recharge = ['data' => $result];

        return $this->jsonSuccess($recharge);
    }

    /**
     * 余额查询
     */
    public function balance()
    {
        $balance = model('user')->balance(['id' => $this->curUser['id']]);
        if (isset($balance['money'])) {
            $result = [
                'data' => [
                    'money' => $balance['money'],
                    ],
            ];

            return $this->jsonSuccess($result);
        } else {
            return $this->jsonError('余额查询失败，请重试');
        }
    }

    /**
     * 会员下注量
     */
    public function betCount()
    {
        $lastTime = $this->request->get('lastTime', '');
        if (empty($lastTime)){
            return $this->jsonError('上次时间不能为空');
        }

        $result = [];
        $userModel = model('user');
        $userBet = $userModel->getRow(['user_login' => $this->curUser['user_login']], 'id, user_login, bet_count');

        if (! empty($userBet)){
            $result = [
                'uname' => $userBet['user_login'],
                'dama' => intval($userBet['bet_count']),
            ];
            $queryData = [
                'query_time' => time(),
            ];
            try {
                $userModel->save($queryData, ['id' => $userBet['id']]);
            } catch (Exception $e){
                return $this->jsonError('更新查询时间出错，请重试 ' . $e->getMessage());
            }
        } else {
            $condition = [
                'field' => 'user_login AS uname, bet_count AS dama',
                'page' => intval($this->request->get('page')),
                'page_size' => intval($this->request->get('pgsize')),
            ];
            $result = $userModel->getList($condition);
        }

        $betCount = [
            'msg' => 'SUC',
            'data' => $result,
            'endTime' => $lastTime,
        ];

        return $this->jsonSuccess($betCount);
    }

    /**
     * 退出登陆
     * @return string
     */
    public function logout()
    {
        Session::clear();
        Cookie::clear();

        return $this->jsonSuccess('退出登陆成功');
    }

}
